package jp.co.cos.ittrick.channel.controller.admin;

import jp.co.cos.ittrick.channel.model.PageModel;
import jp.co.cos.ittrick.channel.service.FileService;
import jp.co.cos.ittrick.channel.service.PageService;

import org.slim3.controller.Controller;
import org.slim3.controller.Navigation;

import com.google.appengine.api.datastore.Text;

public class PageEditController extends Controller {

    private PageService pageService;
    private FileService fileService;

    public PageEditController() {
        pageService = new PageService();
        fileService = new FileService();
    }

    @Override
    public Navigation run() throws Exception {

        String channel = asString("channel");
        String chapter = asString("chapter");
        String page = asString("page");
        PageModel pageModel = pageService.getPageByKey(Integer.valueOf(page));

        // カスタムタグのエンエスケープ
        String content = pageModel.getContentString();
        pageModel.setContent(new Text(content));

        requestScope("pageModel", pageModel);
        requestScope("channel", channel);
        requestScope("chapter", chapter);
        requestScope("page", page);

        // キーワード
        String keyWord = null;
        for(String keyWordTmp: pageModel.getKeyWordList()) {
            if(keyWord == null) {
                keyWord = keyWordTmp;
            }else {
                keyWord = keyWord + "," + keyWordTmp;
            }
        }
        requestScope("keyWord", keyWord);

        // ファイル
        requestScope("dataList", fileService.getDataListOfPage(Integer.valueOf(page)));

        return forward("pageEdit.jsp");
    }
}
